<?php
class Site_Form_SiteAdd extends Promotor_Form_Scaffold {
	public function init() {
		$this->setMethod('post');
		$this->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);

		$this->addPrefixPath('Promotor_Form_Decorator_','Promotor/Form/Decorator', self::DECORATOR);

		$subForm = $this->_getContentForm();
        $this->addSubForm($subForm, 'contenttab');
        
        $subForm = $this->_getSeoForm();
        $this->addSubForm($subForm, 'seotab');
        
        $subForm = $this->_getExForm();
        $this->addSubForm($subForm, 'exttab');
	}
	
	/**
     * @return Zend_Dojo_Form_SubForm
     */
    protected function _getContentForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'contenttab',
            'legend' => 'Treść',
        ));
        $subForm->addElement(
                'text',
                'name',
                array(
                    'label' => 'Nazwa strony',
                    'required'  => true,
                )
            );            
		$content = new Zend_Form_Element_Textarea('content', array(
			'label'        => 'Zawartość strony',
			'inheritWidth' => 'true',
            'required' 	   =>false,
		));
		$content->addDecorator(new KontorX_Form_Decorator_TinyMCE());
		$subForm->addElement($content);

        return $subForm;
    }
    
    /**
     * @return Zend_Form_SubForm
     */
    protected function _getSeoForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'metatab',
            'legend' => 'Pozycjonowanie'
        ));
        
        $locale = preg_replace('/[^\w]/', '', @$_POST['exttab']['locale']);
        $exclude = sprintf('locale = "%s"', $locale);

        $subForm
        	->addElement(
                'text',
                'alias',
                array(
                    'label' => 'Alias strony',
                    'required'  => false,
                    'regExp' => '[\w\d\-\s]+',
                    'invalidMessage' => 'Tylko tekst bez polskich znaków diakrytycznych!',
                	'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim(),
                		'strtolower' => new Zend_Filter_StringToLower(),
                		'rewrite' => new KontorX_Filter_Word_Rewrite()
                	),
                	'validators' => array(
                		'db_no_exsists' => new Zend_Validate_Db_NoRecordExists('site','alias', $exclude)
                	)
                )
            )
            ->addElement(
                'Textarea',
                'title',
                array(
                    'label'      => 'Tytuł strony',
                    'trim'       => true
				)
            )
            ->addElement(
                'Textarea',
                'meta_keywords',
                array(
                    'label'      => 'Słowa kluczowe',
                    'trim'       => true
				)
            )
            ->addElement(
                'Textarea',
                'meta_description',
                array(
                    'label'      => 'Opis strony',
                    'trim'       => true
                )
            );
            
        return $subForm;
    }
    
	/**
     * @return Zend_Form_SubForm
     */
    protected function _getExForm() {
    	$subForm = new Zend_Form_SubForm();
    	
    	$locale = 'pl';
    	if (Zend_Registry::isRegistered('Zend_Locale')) {
    		/* @var $locale Zend_Locale */
	    	$locale = Zend_Registry::get('Zend_Locale');
	    	if (strlen($val = $locale->getLanguage())) {
	    		$locale = $val; 
	    	}
    	}

        $subForm->setAttribs(array(
            'name'   => 'exttab',
            'legend' => 'Zaawansowane'
        ));
        $subForm
        	->addElement(
                'text',
                'redirect',
                array(
                    'label' => 'Przekierowanie',
                    'required'  => false
                )
            )
            ->addElement(
                'text',
                'locale',
                array(
                    'label' => 'Język',
                    'required'  => true,
                	'value' => $locale
                )
            )
            ->addElement(
            	'radio',
            	'publicated',
            	array(
            		'label' => 'Publikować',
            		'value' => 1,
            		'multiOptions' => array(
            			0 => 'Nie',
            			1 => 'Tak',
            			2 => 'Tak, ale nie pokazuj strony w spisie',
            		)
            	)
            )
            ->addElement(new KontorX_Form_Element_Db_Select('template_id', array(
            	'label' => 'Szablon strony',
                'description' => 'Wybierz szablon graficzny dla tej strony',
            	'required' => false,
            	'tableName' => 'template',
            	'tableCols' => array('key' => 'id','value' => 'name')
            )))
            ->addElement(
            	'checkbox',
            	'display_news',
            	array(
            		'label' => 'Wyświetlaj aktualności'
            	)
            )
            ->addElement(
            	'checkbox',
            	'display_by_date',
            	array(
            		'label' => 'Wyświetlaj według daty'
            	)
            )
            ->addElement(
            	'checkbox',
            	'display_by_type',
            	array(
            		'label' => 'Wyświetlaj według typu'
            	)
            );
            
        return $subForm;
    }
}